package com.lin.gulimall.search.thread;


import java.util.concurrent.*;

public class ThreadTest {

    public static ExecutorService executor = Executors.newFixedThreadPool(5);


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        System.out.println("main....start....");
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10/2;
//            System.out.println("运行结果:"+i);
//        }, executor);

//        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10/0;
//            System.out.println("运行结果:"+i);
//            return i;
//        }, executor).whenComplete((res,excption)->{
//            //虽然能得到异常信息，但是没法修改返回数据
//            System.out.println("异步任务成功完成了...结果是："+res+";异常是:"+excption);
//        }).exceptionally(throwable -> {
//            //可以感知异常，同时返回默认值
//            return 10;
//        });

//            CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10/0;
//            System.out.println("运行结果:"+i);
//            return i;
//        }, executor).handle((res,thr)->{
//            if (res!=null){
//                return res * 2;
//            }
//            if (thr!=null){
//                return 0;
//            }
//            return 0;
//            });

//        CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10/2;
//            System.out.println("运行结果:"+i);
//            return i;
//        }, executor).thenRunAsync(()->{
//            System.out.println("任务二 启动了");
//        },executor);


//        CompletableFuture.supplyAsync(() -> {
//                    System.out.println("当前线程:" + Thread.currentThread().getId());
//                    int i = 10/2;
//                    System.out.println("运行结果:"+i);
//                    return i;
//                }, executor).thenAcceptAsync(res->{
//            System.out.println("任务二启动了..."+res);
//        },executor);


//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + i);
//            return i;
//        }, executor).thenApplyAsync((res) -> {
//            return res * 2;
//        });



//        CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务1线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("任务1结束:" );
//            return i;
//        }, executor);
//
//            CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2线程:" + Thread.currentThread().getId());
//            try {
//                Thread.sleep(3000);
//                System.out.println("任务2结束:");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            return "hello";
//        }, executor);

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jpg";
        });

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片属性");
            return "黑色256G";
        });

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查询商品的图片介绍");
            return "华为";
        });

        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        anyOf.get();


        System.out.println("main...end..."+anyOf.get());

//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
//          .get()  等待所有结果完成
//        allOf.get()
//        CompletableFuture<String> future = future1.applyToEitherAsync(future2, (res) -> {
//            return res.toString() + "->哈哈";
//        }, executor);
//        future1.acceptEitherAsync(future2,(res)->{
//            System.out.println("任务3开始,,,之前的结果"+res);
//        },executor);

//        future1.runAfterEitherAsync(future2,()->{
//            System.out.println("任务3开始...之前的结果:");
//        },executor);

//        CompletableFuture<String> future = future1.thenCombineAsync(future2, (f1, f2) -> {
//            return f1 + ":" + f2 + "->Hahaha";
//        }, executor);

//        future1.thenAcceptBothAsync(future2,(f1,f2)->{
//            System.out.println("任务3开始...之前的结果:"+f1+"..>"+f2);
//        },executor);

//        future1.runAfterBothAsync(future2,()->{
//            System.out.println("任务3开始...");
//        },executor);



    }

}
